package com.mdd.admin.tools;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;

import java.util.HashSet;
import java.util.Map;
import java.util.Random;
import java.util.Set;

/**
 * 生成燃油车车牌
 */
public class VehicleRuleUtil {

    /**
     * 随机生成五位数字车牌号
     *
     * @return
     */
    public static String exeVehicleNumber() {
        Random random = new Random();
        String tempV = "";
        do {
            int i = random.nextInt(VehicleConstants.STR_ZERO_NICE.length);
            //重置车牌
            if (tempV.length() == 5) {
                tempV = "";
            }
            tempV += VehicleConstants.STR_ZERO_NICE[i];
        } while (tempV.length() < 5 || checkVipVehicle(tempV));
        return tempV;
    }

    /**
     * 牌照后五位(随机生成车牌数字+字母或者全数字)
     *
     * @param num 需要生成多少个车牌
     * @return
     */
    public static Set<String> vehicleRandomFIVE(int num) {
        Set<String> vehicleSet = new HashSet<>();
        String tempVehicle;//车牌
        Random random = new Random();
        while (vehicleSet.size() < num) {
            do {
                //再次循环需要初始化车牌
                tempVehicle = "";
                //牌照后五位
                for (int j = 0; j < 5; j++) {
                    int i = random.nextInt(VehicleConstants.STR_AZ_NICE.length);
                    tempVehicle += VehicleConstants.STR_AZ_NICE[i];
                }
                //如果牌照后五位大于2个字母则从新随机组成后五位
            } while (checkVehicleLetter(tempVehicle));

            //排除特殊号码
            if (!checkVipVehicle(tempVehicle)) {
                vehicleSet.add(tempVehicle);
            }
        }
        return vehicleSet;
    }

    /**
     * 牌照后五位(随机)
     * 两位字母紧靠
     * 三位数字紧靠
     *
     * @param num 需要生成多少个车牌
     * @param b   true 字母在前，数字在后
     * @return
     */
    public static Set<String> vehicleRandom(int num, boolean b) {
        Set<String> vehicleSet = new HashSet<>();
        String tempVehicle;//车牌
        Random random = new Random();
        while (vehicleSet.size() < num) {
            //生成两位字母
            int i1 = 0, i2 = 0;
            while (i1 == i2) {
                i1 = random.nextInt(VehicleConstants.STR_AZ.length);
                i2 = random.nextInt(VehicleConstants.STR_AZ.length);
            }
            String strA = VehicleConstants.STR_AZ[i1];
            String strZ = VehicleConstants.STR_AZ[i2];
            String vehicleA_Z = strA + strZ;

            //生成三位数字
            int j1 = 0, j2 = 0, j3 = 0;
            while (j1 == j2 || j2 == j3) {
                j1 = random.nextInt(VehicleConstants.STR_ZERO_NICE.length);
                j2 = random.nextInt(VehicleConstants.STR_ZERO_NICE.length);
                j3 = random.nextInt(VehicleConstants.STR_ZERO_NICE.length);
            }
            String strZERO = VehicleConstants.STR_ZERO_NICE[j1];
            String strONE = VehicleConstants.STR_ZERO_NICE[j2];
            String strTWO = VehicleConstants.STR_ZERO_NICE[j3];
            String vehicle0_9 = strZERO + strONE + strTWO;

            tempVehicle = b ? vehicleA_Z + vehicle0_9 : vehicle0_9 + vehicleA_Z;

            //排除特殊号码
            if (!checkVipVehicle(tempVehicle)) {
                vehicleSet.add(tempVehicle);
            }
        }
        return vehicleSet;
    }

    /**
     * 指定车牌号码(数字加字母)
     * 指定车牌小于3位时候会校验特殊敏感车牌
     *
     * @param appMap key 位数  value   车牌号码值
     * @param boo    是否要带字母    true 带 false不带
     * @param bAZ    true带2个字母 false带一个字母
     * @return
     */
    public static String vehicleAppoint(Map<Integer, String> appMap, boolean boo, boolean bAZ) {
        if (null == appMap || appMap.isEmpty()) {
            System.out.println("*******指定车牌至少有一位数字或者字母******");
            return null;
        }
        //指定车牌所有值
        if (appMap.size() == 5) {
            return buildMap(appMap);
        }
        //指定车牌未超过三位数则校验是否包含特殊敏感车牌
        boolean bl = false;
        if (appMap.size() < 3) {
            bl = true;
        }

        String vehicleTemp = buildMap(appMap);
        //使用StringBuilder避免在字符串中创建新的对象
        StringBuilder stringBuilder = new StringBuilder(vehicleTemp);
        //字母个数
        int strI = strCount(stringBuilder.toString());
        //生成两个字母做记录
        int inAZ = 0;
        //处理需要生成几个字母
        if (strI == 2) {
            boo = false;
        } else if (strI == 1 && boo) {
            bAZ = false;
        }
        //初始化需要生成字母的个数记录
        if (boo && bAZ) {
            inAZ = 2;
        } else if (boo && !bAZ) {
            inAZ = 1;
        }
        do {
            //初始化车牌
            if (stringBuilder.indexOf(VehicleConstants.STR_SYS) < 0) {
                stringBuilder = new StringBuilder(vehicleTemp);
                if (boo && bAZ) {
                    inAZ = 2;
                } else if (boo && !bAZ) {
                    inAZ = 1;
                }
            }
            for (int i = 0; i < stringBuilder.length(); i++) {
                if (VehicleConstants.STR_SYS.equals(String.valueOf(stringBuilder.charAt(i)))) {
                    //生成一个字母
                    if (boo && !bAZ) {
                        String s = executeONE(false);
                        stringBuilder.setCharAt(i, s.charAt(0));
                        boo = false;
                        //生成两个字母
                    } else if (boo && bAZ && inAZ != 0) {
                        String s = executeONE(false);
                        stringBuilder.setCharAt(i, s.charAt(0));
                        inAZ--;
                        //全是数字
                    } else if (!boo || inAZ == 0) {
                        String s = executeONE(true);
                        stringBuilder.setCharAt(i, s.charAt(0));
                    }
                }
            }
        } while (otherCount(stringBuilder.toString()) > 0 || (bl && checkVipVehicle(stringBuilder.toString())) || checkVehicleLetter(stringBuilder.toString()));

        return stringBuilder.toString();
    }


    /**
     * VIP号码    炸弹号，顺子号
     * 这里加一个记录输入VIP次数的记录，例如输入了3次或者三次以上
     * 做锁定操作，或者提示你这样做很危险，或者加入黑名单等等等等
     *
     * @param vipKey
     * @return
     */
    public static String vehicleVIP(String vipKey) {
        String vehicleStr = VehicleConstants.VEHICLE_BOMB_MAP.get(vipKey);
        if (null == vehicleStr) {
            vehicleStr = VehicleConstants.VEHICLE_STRAIGHT_ASC_MAP.get(vipKey) == null ? VehicleConstants.VEHICLE_STRAIGHT_ASC_MAP.get(vipKey) : VehicleConstants.VEHICLE_STRAIGHT_DESC_MAP.get(vipKey);
        }
        if (null == vehicleStr) {
            System.out.println("你小子在这跟我玩呢！");
            System.out.println("没有这个VIP等级：" + vipKey);
            System.out.println("只有下面这些，看清楚了！");
            vehicleShowVIP();
        }
        return vehicleStr;
    }

    /**
     * 判断是否在VIP号码段
     *
     * @param tempVehicle
     */
    private static boolean checkVipVehicle(String tempVehicle) {
        boolean b = checkVehicleSensitive(tempVehicle);
        boolean b1 = VehicleConstants.VEHICLE_BOMB_MAP.values().contains(tempVehicle);
        boolean b2 = VehicleConstants.VEHICLE_STRAIGHT_ASC_MAP.values().contains(tempVehicle);
        boolean b3 = VehicleConstants.VEHICLE_STRAIGHT_DESC_MAP.values().contains(tempVehicle);
        boolean b4 = checkVehicleDIS(tempVehicle);
        return b || b1 || b2 || b3 || b4;
    }

    /**
     * 随机生成一位数字或者字母
     *
     * @param b true数字  false 字母
     * @return
     */
    public static String executeONE(boolean b) {
        Random random = new Random();
        String tempStr;
        if (b) {
            tempStr = VehicleConstants.STR_ZERO_NICE[random.nextInt(VehicleConstants.STR_ZERO_NICE.length)];
        } else {
            tempStr = VehicleConstants.STR_AZ[random.nextInt(VehicleConstants.STR_AZ.length)];
        }
        return tempStr;
    }

    /**
     * 展示VIP号码  炸弹号，顺子号
     * 如果被人选走就不做展示了，这里要坐下排除
     */
    public static void vehicleShowVIP() {
        System.out.println("===========炸弹号==========");
        VehicleConstants.VEHICLE_BOMB_MAP.forEach((k, v) -> System.out.println("VIP等级【" + k + "】*****VIP号码:" + v));
        System.out.println("===========顺子号码==========");
        VehicleConstants.VEHICLE_STRAIGHT_ASC_MAP.forEach((k, v) -> System.out.println("VIP等级【" + k + "】*****VIP号码:" + v));
        VehicleConstants.VEHICLE_STRAIGHT_DESC_MAP.forEach((k, v) -> System.out.println("VIP等级【" + k + "】*****VIP号码:" + v));
    }

    /**
     * 生成两位字母，字母是紧靠一起的(可指定)
     *
     * @param str1 字母1
     * @param str2 字母2
     * @return
     */
    public static String executeA_Z(String str1, String str2) {
        if (!StringUtils.isBlank(str1) && !StringUtils.isBlank(str2)) {
            if (str1.equals(str2)) {
                System.out.println("***********两个字母不允许相同************");
            }
            if (VehicleConstants.LIST_IO.contains(str1) || VehicleConstants.LIST_IO.contains(str2)) {
                System.out.println("***********指定的字母不允许是I或者O************");
            }
            if (strCount(str1) == 0) {
                System.out.println("***********指定车牌第一个必须为字母***********");
            }
            if (strCount(str2) == 0) {
                System.out.println("***********指定车牌第二个必须为字母***********");
            }
        }
        Random random = new Random();
        //生成两位字母
        int i1, i2;
        do {
            i1 = random.nextInt(VehicleConstants.STR_AZ.length);
            i2 = random.nextInt(VehicleConstants.STR_AZ.length);
        } while (i1 == i2);
        if (!StringUtils.isBlank(str1) && StringUtils.isBlank(str2)) {
            //判断是否为字母
            if (strCount(str1) == 0) {
                System.out.println("***********指定车牌1必须为字母***********");
            }
            str2 = VehicleConstants.STR_AZ[i2];
            while (checkDubboA_Z(str1 + str2)) {
                int i = random.nextInt(VehicleConstants.STR_AZ.length);
                str2 = VehicleConstants.STR_AZ[i];
            }
            return str1 + str2;
        } else if (!StringUtils.isBlank(str2) && StringUtils.isBlank(str1)) {
            //判断是否为字母
            if (strCount(str2) == 0) {
                System.out.println("***********指定车牌2必须为字母***********");
            }
            str1 = VehicleConstants.STR_AZ[i2];
            while (checkDubboA_Z(str1 + str2)) {
                int i = random.nextInt(VehicleConstants.STR_AZ.length);
                str1 = VehicleConstants.STR_AZ[i];
            }
            return str1 + str2;
        } else if (StringUtils.isBlank(str1) && StringUtils.isBlank(str2)) {
            str1 = VehicleConstants.STR_AZ[i1];
            str2 = VehicleConstants.STR_AZ[i2];
        }
        return str1 + str2;
    }

    /**
     * 生成三位数字，数字是紧靠一起的(可指定)
     *
     * @param str1 数字1
     * @param str2 数字2
     * @param str3 数字3
     * @return
     */
    public static String execute0_9(String str1, String str2, String str3) {
        Random random = new Random();
        int j1, j2, j3;
        j1 = random.nextInt(VehicleConstants.STR_ZERO_NICE.length);
        j2 = random.nextInt(VehicleConstants.STR_ZERO_NICE.length);
        j3 = random.nextInt(VehicleConstants.STR_ZERO_NICE.length);
        if (StringUtils.isBlank(str1)) {
            str1 = VehicleConstants.STR_ZERO_NICE[j1];
        } else {
            if (numberCount(str1) == 0) {
                System.out.println("指定车牌1必须为数字");
            }
        }
        if (StringUtils.isBlank(str2)) {
            str2 = VehicleConstants.STR_ZERO_NICE[j2];
        } else {
            if (numberCount(str2) == 0) {
                System.out.println("指定车牌2必须为数字");
            }
        }
        if (StringUtils.isBlank(str3)) {
            str3 = VehicleConstants.STR_ZERO_NICE[j3];
        } else {
            if (numberCount(str3) == 0) {
                System.out.println("指定车牌3必须为数字");
            }
        }
        return str1 + str2 + str3;
    }

    /**
     * 判断车牌中的字母是否大于2或者字母是否相同
     *
     * @param tempVehicle
     * @return
     */
    public static boolean checkVehicleLetter(String tempVehicle) {
        int i = strCount(tempVehicle);
        boolean b = checkDubboA_Z(tempVehicle);
        return i > 2 || b;
    }

    /**
     * 判断有多少个数字
     *
     * @param str
     * @return
     */
    public static int numberCount(String str) {
        int num = 0;
        for (int i = 0; i < str.length(); i++) {
            if (str.charAt(i) >= '0' && str.charAt(i) <= '9') {
                num++;
            }
        }
        return num;
    }

    /**
     * 判断有多少字母
     *
     * @param str
     * @return
     */
    public static int strCount(String str) {
        int num = 0;
        for (int i = 0; i < str.length(); i++) {
            if ((str.charAt(i) >= 'A' && str.charAt(i) <= 'Z') || (str.charAt(i) >= 'a' && str.charAt(i) <= 'z')) {
                num++;
            }
        }
        return num;
    }

    /**
     * 判断车牌中的两个字母是否相同
     * 只用于燃油车车牌
     *
     * @param str
     * @return
     */
    public static boolean checkDubboA_Z(String str) {
        boolean b = false;
        String tempA = null, tempB = null;
        for (int i = 0; i < str.length(); i++) {
            if ((str.charAt(i) >= 'A' && str.charAt(i) <= 'Z') || (str.charAt(i) >= 'a' && str.charAt(i) <= 'z')) {
                if (null == tempA) {
                    tempA = String.valueOf(str.charAt(i));
                } else {
                    tempB = String.valueOf(str.charAt(i));
                }
            }
        }
        if (null != tempA && null != tempB) {
            b = tempA.equals(tempB);
        }
        return b;
    }

    /**
     * 判断是字母或者是数字
     * 两者都不是则为false
     *
     * @param str
     * @return
     */
    public static boolean checkStr(String str) {
        //特殊符号
        if (VehicleConstants.STR_SYS.equals(str)) {
            return true;
        }
        int i = numberCount(str);
        int i1 = strCount(str);
        return i == 1 || i1 == 1;
    }

    /**
     * 排序指定车牌
     *
     * @param appMap
     * @return
     */
    public static String buildMap(Map<Integer, String> appMap) {
        String s1 = appMap.get(1) == null ? "*" : appMap.get(1);
        String s2 = appMap.get(2) == null ? "*" : appMap.get(2);
        String s3 = appMap.get(3) == null ? "*" : appMap.get(3);
        String s4 = appMap.get(4) == null ? "*" : appMap.get(4);
        String s5 = appMap.get(5) == null ? "*" : appMap.get(5);
        boolean b = checkStr(s1) && checkStr(s2) && checkStr(s3) && checkStr(s4) && checkStr(s5);
        String tempS = s1 + s2 + s3 + s4 + s5;
        if (!b) {
            System.out.println("*******请填写正取的车牌数字或者字母*****" + tempS);
        }
        if (tempS.contains(VehicleConstants.STR_I) || tempS.contains(VehicleConstants.STR_O)) {
            System.out.println("***********指定的字母不允许是I或者O************" + tempS);
        }
        boolean bl = checkVehicleLetter(tempS);
        if (bl) {
            System.out.println("*******指定车牌大于2个字母,或者字母相同******" + tempS);
        }
        return bl ? null : tempS;
    }

    /**
     * 是否包含敏感车牌
     *
     * @param tempVehicle
     * @return
     */
    public static boolean checkVehicleSensitive(String tempVehicle) {
        for (String str : VehicleConstants.VEHICLE_SENSITIVE_LIST) {
            int i = tempVehicle.indexOf(str);
            if (i >= 0) {
                System.out.println("车牌【" + tempVehicle + "】随机生成的不允许有特殊敏感车牌号码" + str);
                return true;
            }
        }
        return false;
    }

    /**
     * 判断车牌（特殊车牌）
     * 是否是AABAA
     * 或者ABCAB
     *
     * @return
     */
    public static boolean checkVehicleDIS(String tempVehicle) {
        String begin = tempVehicle.substring(0, 2);
        String end = tempVehicle.substring(3);
        return begin.equals(end);
    }

    /**
     * 判断包含出数字和字母外的特殊字符数量
     *
     * @param str
     * @return
     */
    public static int otherCount(String str) {
        int num = 0;
        for (int i = 0; i < str.length(); i++) {
            if (str.charAt(i) >= '0' && str.charAt(i) <= '9') {
                continue;
            } else if (str.charAt(i) >= 'a' && str.charAt(i) <= 'z') {
                continue;
            } else if (str.charAt(i) >= 'A' && str.charAt(i) <= 'Z') {
                continue;
            } else {
                num++;
            }
        }
        return num;
    }

}
